$(function(){
    $("#topBtn").click(setTop);
    $("#reTopBtn").click(setReTop);
    $("#wonderfulBtn").click(setWonderful);
    $("#reWonderfulBtn").click(setReWonderful);
    $("#deleteBtn").click(setDelete);
});

// 点赞
function like(btn, entityType, entityId, entityUserId, postId) {
    $.post(
        CONTEXT_PATH + "/like",
        {"entityType":entityType, "entityId":entityId, "entityUserId":entityUserId, "postId":postId },
        function(data) {
            data = $.parseJSON(data);
            if (data.code === 0) {
                if(data.likeStatus === 1){
                    layer.msg("已点赞！", {icon: 1});
                }else{
                    layer.msg("已取消点赞！", {icon: 1});
                }

                $(btn).children("i").text(data.likeCount);
                $(btn).children("b").text(data.likeStatus === 1 ? '已赞' : '赞');
            }
            else {
                layer.msg("操作失败", {icon: 2});
            }

        }
    )
}

// 置顶
function setTop() {
    $.post(
        CONTEXT_PATH + "/discuss/top",
        {"id":$("#postId").val(),"type":1},
        function (data) {
            data = $.parseJSON(data);
            if (data.code === 0) {
                layer.msg(data.msg, {icon: 1});
                // 置顶成功后刷新页面
                setTimeout(function(){
                    window.location.reload();
                },1000);
            }
            else {
                layer.msg(data.msg, {icon: 2});
            }
        }
    )
}
// 取消置顶
function setReTop() {
    $.post(
        CONTEXT_PATH + "/discuss/top",
        {"id":$("#postId").val(),"type":0},
        function (data) {
            data = $.parseJSON(data);
            if (data.code === 0) {
                layer.msg(data.msg, {icon: 1});
                setTimeout(function(){
                    window.location.reload();
                },1000);
            }
            else {
                layer.msg(data.msg, {icon: 2});
            }
        }
    )
}

// 加精
function setWonderful() {
    $.post(
        CONTEXT_PATH + "/discuss/wonderful",
        {"id":$("#postId").val(),"status":1},
        function (data) {
            data = $.parseJSON(data);
            if (data.code === 0) {
                layer.msg(data.msg, {icon: 1});
                setTimeout(function(){
                    window.location.reload();
                },1000);
            }
            else {
                layer.msg(data.msg, {icon: 2});
            }
        }
    )
}

// 取消加精
function setReWonderful() {
    $.post(
        CONTEXT_PATH + "/discuss/wonderful",
        {"id":$("#postId").val(),"status":0},
        function (data) {
            data = $.parseJSON(data);
            if (data.code === 0) {
                layer.msg(data.msg, {icon: 1});
                setTimeout(function(){
                    window.location.reload();
                },1000);
            }
            else {
                layer.msg(data.msg, {icon: 2});
            }
        }
    )
}

// 删除
function setDelete() {
    $.post(
        CONTEXT_PATH + "/discuss/delete",
        {"id":$("#postId").val()},
        function (data) {
            data = $.parseJSON(data);
            if (data.code === 0) {
                layer.msg(data.msg, {icon: 1});
                // 删除成功后，跳转到首页
                setTimeout(function(){
                    location.href = CONTEXT_PATH + "/index";
                },800);
            }
            else {
                layer.msg(data.msg, {icon: 2});
            }
        }
    )
}
